var app = getApp()
var axios = require("../../utils/core")
var timer = require("../../utils/util")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentTab: 0,
        listnav: ["推荐收益", "分销收益", "供应链收益"],
        openid: null,
        last_page: 1,
        page:1,
        list: {},
        type: 0, //0分红佣金，1佣金
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        var openid = wx.getStorageSync("openid")
        that.setData({
            openid: openid,
            currentTab: 0
        })
        let arr = options.type == 1 ? ["分销收益", "供应链收益"] : ["在途收益", "已结算收益", "供应链收益"] //0分红佣金，1佣金
        that.setData({
            listnav: arr,
            type: options.type
        })
        console.log(that.data.type, 'curType');
        if (that.data.type == 1) {
            axios.get("commission.order.get_list", {page:that.data.page}, ((e) => {
                console.log(e);
                that.setData({
                    list: e.list,
                    last_page: Math.ceil(e.total / e.pagesize)
                })
            }))
        } else {
            axios.get("goods.tjprice", {
                type: 1
            }, ((e) => {
                console.log(e);
                e.list.forEach((item) => {
                    let time = item.createtime
                    time = timer.formatTimeTwo(time, 'Y/M/D h:m:s')
                    item.createtime = time
                });
                that.setData({
                    list: e.list
                })

                // console.log(e);
                // that.setData({
                //     list: e.list
                // })
            }))
        }

        // axios.post("goods.tjprice", {
        //     openid: openid
        // }, ((e) => {
        //     console.log(e);
        //     e.list.forEach((item) => {
        //         console.log(item.createtime);
        //         let time = item.createtime
        //         time = timer.formatTimeTwo(time, 'Y/M/D h:m:s')
        //         item.createtime = time
        //     });
        //     that.setData({
        //         list: e.list
        //     })
        //     if (e.message) {
        //         wx.showModal({
        //             cancelColor: 'cancelColor',
        //             content: e.message,
        //             showCancel: false,
        //             success(res) {
        //                 if (res.confirm) {
        //                     wx.switchTab({
        //                         url: '/pages/index/index',
        //                     })
        //                 }
        //             }
        //         })
        //     }
        // }))
    },
    changenav(e) {
        var that = this
        var cur = e.currentTarget.dataset.current
        that.setData({
            currentTab: cur,
            list: [],
            last_page: 1
        })
        console.log(that.data.type)
        if (that.data.type == 0) {
            this.shareCommission()
        } else {
            this.commission()
        }
    },
    shareCommission() { //分红佣金
        let that = this
        if (this.data.currentTab == 0) {
            axios.post("goods.tjprice", {
                type: 1
            }, ((e) => {
                console.log(e);
                e.list.forEach((item) => {
                    let time = item.createtime
                    time = timer.formatTimeTwo(time, 'Y/M/D h:m:s')
                    item.createtime = time
                });
                that.setData({
                    list: e.list
                })
            }))
        } else if (this.data.currentTab == 1) {
            //    commission.order.get_list
            axios.get("goods.tjprice", {}, ((e) => {
                console.log(e);
                e.list.forEach((item) => {
                    let time = item.createtime
                    time = timer.formatTimeTwo(time, 'Y/M/D h:m:s')
                    item.createtime = time
                });
                that.setData({
                    list: e.list
                })
            }))
        } else if (this.data.currentTab == 2) {
            axios.get("goods.gylprice", {}, ((e) => {
                console.log(e);
                e.list.forEach((item) => {
                    let time = item.createtime
                    time = timer.formatTimeTwo(time, 'Y/M/D h:m:s')
                    item.createtime = time
                });
                that.setData({
                    list: e.list
                })
            }))
        }
    },
    commission() { //佣金type为1的时候
        let that = this
        if (this.data.currentTab == 0) {
            axios.get("commission.order.get_list", {
                page: this.data.page
            }, ((e) => {
                console.log(e);
                that.setData({
                    list: that.data.list.concat(e.list),
                    last_page: Math.ceil(e.total / e.pagesize)
                })
            }))
        } else if (this.data.currentTab == 1) {
            axios.get("goods.gylprice", {}, ((e) => {
                console.log(e);
                e.list.forEach((item) => {
                    let time = item.createtime
                    time = timer.formatTimeTwo(time, 'Y/M/D h:m:s')
                    item.createtime = time
                });
                that.setData({
                    list: e.list
                })
            }))
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.type == 1) {
            if (this.data.currentTab == 0) {
                if (this.data.page < this.data.last_page) {
                    this.data.page = this.data.page + 1
                    this.commission()
                }
            }
        }
    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage: function () {

    // }
})